/*
 * WaitingList.fx
 * Author: Yu Min-ting
 * Created on 2009-7-21, 10:08:05
 */

package com.sjtu.fx.book;

import javafx.scene.*;
import com.sjtu.fx.tools.Panel;
import javafx.scene.control.Button;

import javafx.scene.layout.HBox;

import com.sjtu.fx.user.Login;
import javafx.scene.control.Label;

import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import javafx.scene.layout.VBox;


import java.lang.System;

import javafx.io.http.HttpRequest;
import javafx.stage.Alert;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONObject;


import com.sjtu.fx.tools.SwingTable;


import com.sjtu.fx.tools.UIAlert;

import com.sjtu.fx.tools.SwingTable.TableCell;
import com.sjtu.fx.tools.SwingTable.TableColumn;
import com.sjtu.fx.tools.SwingTable.TableRow;

public class BookLibraryInfo{
    public var libraryName:String;
    public var Numberinfo:String;
}

public class WaitingList extends CustomNode{
     var panel:Panel;
     public var selection: Integer;
     public var availableNumber:Integer = -1 on replace{
         if(availableNumber>0){
             borrow.disable = false;
             addMe.disable = true;
         }else{
             borrow.disable = true;
             addMe.disable = false;
         }


         };
     public var bookInfo:BookLibraryInfo[] = bind bookInfo with inverse;


     var borrow = Button {
             text: "      Borrow Now      "
             action: function() {
                 var userId:Integer = (parent.parent.parent.lookup("loginPanel") as Login).userid;
                 var bookId:Integer = (parent.parent.parent.lookup("borrowBook") as BookDetails).bookId;
                 var libraryName:String = bookInfo[selection].libraryName;
                 System.out.println(libraryName);
                 borrowBook(userId,bookId,libraryName);
             }
         }
     var addMe = Button {
             text: "Add me to waiting List"
             disable:true
             action: function() {
                 var userId:Integer = (parent.parent.parent.lookup("loginPanel") as Login).userid;
                 var bookId:Integer = (parent.parent.parent.lookup("borrowBook") as BookDetails).bookId;
                 addWaitingList(userId,bookId);
             }
         }

     var borrowInfo = Label {
             text: "Available book numbers : "
             font: Font {size:12 name: "Verdana"}
             textFill: Color.rgb(11,51,60)
         }
     var numberInfo = Label {
             text:  bind availableNumber.toString()
             font: Font {size:12 name: "Verdana Bold"}
             textFill: Color.rgb(11,51,60)
         }
        var bookTable:SwingTable = SwingTable{
        columns: [
                    TableColumn {
                        text: "Library Name"
                    },
                    TableColumn {
                        text: "Available/Total Number"
                    },
                ]
        rows: bind for (u in bookInfo)
                    TableRow{
                        cells:[
                            TableCell {
                                text:bind u.libraryName
                            },
                            TableCell {
                                text:bind u.Numberinfo
                            },
                        ]
                    }
        selection: bind selection with inverse

    }

     var alert = UIAlert {
         translateX:75
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9
     }
     override protected function create() : Node{

          Group{
            content:[
                panel = Panel {
                    width:    780
                    height:   305
                    title:    "Waiting List"
                    translateX: 0
                    translateY: 0
                },
                HBox{
                    spacing:50
                    translateX:20 translateY:50
                    content:[bookTable,
                     VBox{
                        spacing:30
                        translateX:10
                        translateY:60
                        content:[
                            HBox{
                                spacing:10
                                content:[borrowInfo,numberInfo]
                            },
                            HBox{
                                spacing: 30
                                content:[borrow,addMe]
                            }
                            ]

                    }

                    ]
                },alert

               

                

                ]
          }
     }

function borrowBook(userId:Integer,bookId:Integer,LibraryName:String){
    System.out.println("userId{userId}bookId{bookId}");
     HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/BorrowBook"
       onOutput: function(os:OutputStream) {
           if(userId == 0){
               alert.title = "warninig";
               alert.content = "You must login first!";
               alert.visible = true;
               //Alert.inform("warning", "you must login first");
           }
           else{
            try {
                var json:JSONObject = JSONObject{};
                json.put("userId",userId);
                json.put("bookId", bookId);
                json.put("libraryName", LibraryName);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
            }
        }
        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                var result:String = data.readLine();
                if(result.equals("Borrow Book Successfully")){
                    alert.title = "Success";
                    alert.content = result;
                    alert.visible = true;
                    //Alert.inform(result);
                }else{
                    alert.title = "Error";
                    alert.content = "something happen to the server";
                    alert.visible = true;
                    //Alert.inform("something happen to the server.");
                }
            } finally {
                is.close();
            }
        }
    }.start();
    }

    function addWaitingList(userId:Integer,bookId:Integer){
    System.out.println("userId{userId}bookId{bookId}");
     HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/addWaitingList"
       onOutput: function(os:OutputStream) {
           if(userId == 0){
               alert.title = "warninig";
               alert.content = "You must login first!";
               alert.visible = true;
               //Alert.inform("warning", "you must login first");
           }
           else{
            try {
                var json:JSONObject = JSONObject{};
                json.put("userId",userId);
                json.put("bookId", bookId);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
            }
        }
        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                var result:String = data.readLine();
                if(result.equals("true")){
                    alert.title = "Success";
                    alert.content = result;
                    alert.visible = true;
                    //Alert.inform(result);
                }else{
                    alert.title = "Error";
                    alert.content = "something happen to the server";
                    alert.visible = true;
                    //Alert.inform("something happen to the server.");
                }
            } finally {
                is.close();
            }
        }
    }.start();
    }

}
